import os.path

from aiogram import Bot, types
from aiogram.types import FSInputFile
from aiogram_dialog import DialogManager
from fluentogram import TranslatorRunner

from poker_bot.config import Settings
from poker_bot.core.constants import ASSETS_FOLDER
from poker_bot.database.services import AccountService, UserService
from poker_bot.tg_bot.services.tg_helpers import (
    answer_msg_with_autodelete,
    send_tg_message,
)


async def on_referral_error(message: types.Message, _, manager: DialogManager, __):
    locale: TranslatorRunner = manager.middleware_data.get("locale")

    await message.answer_photo(
        FSInputFile(os.path.join(ASSETS_FOLDER, "referral_id_example.jpg")),
        caption=locale.get("wrong-account-id"),
    )
    return


async def add_referral(
    message: types.Message,
    _,
    manager: DialogManager,
    referral_account_id: str | int,
    **kwargs
):
    """Добавляем реферала пользователя"""

    bot: Bot = manager.middleware_data.get("bot")
    config: Settings = manager.middleware_data.get("config")
    locale: TranslatorRunner = manager.middleware_data.get("locale")
    user_service: UserService = manager.middleware_data.get("user_service")
    account_service: AccountService = manager.middleware_data.get("account_service")

    if not 6 <= len(str(referral_account_id)) <= 8:
        await message.answer_photo(
            FSInputFile(os.path.join(ASSETS_FOLDER, "id_example.jpg")),
            caption=locale.get("wrong-account-id"),
        )
        return

    user = await user_service.get_user_by_telegram_id(message.from_user.id)
    account = await account_service.get_account_by_id(user.selected_account_id)

    referral_user = await user_service.get_user_by_account_account_id(
        referral_account_id
    )
    if not referral_user:
        await answer_msg_with_autodelete(message, locale.get("no-user-in-club-msg"))
        return
    elif referral_user.telegram_id == message.from_user.id:
        await answer_msg_with_autodelete(message, locale.get("its-your-account-msg"))
        return
    elif referral_user.referrer_id:
        await answer_msg_with_autodelete(
            message, locale.get("user-already-have-referrer-msg")
        )
        return
    elif referral_user.referrer_id == user.id or user.referrer_id == referral_user.id:
        # Исключаем возможность взаимной подписки
        await answer_msg_with_autodelete(
            message, locale.get("cant-add-this-referral-msg")
        )
        return
    await user_service.add_referral(message.from_user.id, referral_user.telegram_id)

    await send_tg_message(
        bot,
        config.tg_bot.admin_channel_id,
        locale.get(
            "new-referral-msg",
            account_id=str(account.account_id),
            username=user.username,
            referral_account_id=str(referral_account_id),
            referral_username=referral_user.username,
        ),
    )
    await message.answer(locale.get("moderation-msg"))
    await manager.done()
